package com.vst.serverdatalib;

import org.json.JSONObject;

import com.vst.serverdatalib.entity.ServerConfigEntity;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

public class SharedPreHelper {
    public final static int SP_TYPE_INT = 0x22;
    public final static int SP_TYPE_STR = SP_TYPE_INT + 1;
    public final static int SP_TYPE_FLOAT = SP_TYPE_STR + 1;
    public final static int SP_TYPE_LONG = SP_TYPE_FLOAT + 1;



    static final String SP_KEY_SERVER_CONTENT = "serverContent";


    public static SharedPreferences getSp(Context context) {
        return context.getApplicationContext().getSharedPreferences(Constans.SHARED_NAME, Context.MODE_MULTI_PROCESS);
    }

    static void saveInfo2Sp(Context ctx, String key, Object value, int type) {
        try {
            Editor editor = getSp(ctx).edit();
            switch (type) {
            case SP_TYPE_FLOAT:
                editor.putFloat(key, (Float) value);
                break;
            case SP_TYPE_STR:
                editor.putString(key, (String) value);
                break;
            case SP_TYPE_INT:
                editor.putInt(key, (Integer) value);
                break;
            case SP_TYPE_LONG:
                editor.putLong(key, (Long) value);
                break;

            default:
                editor.putString(key, (String) value);
                break;
            }
            editor.commit();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    static void saveInfo2Sp(Context ctx, String key, Object value) {
        saveInfo2Sp(ctx, key, value, SP_TYPE_STR);
    }

    static Object getInfoFromSp(Context ctx, String key) {
        return getInfoFromSp(ctx, key, SP_TYPE_STR);
    }

    static Object getInfoFromSp(Context ctx, String key, int type) {
        try {
            Object o = null;
            switch (type) {
            case SP_TYPE_FLOAT:
                o = getSp(ctx).getFloat(key, 0);
                break;
            case SP_TYPE_STR:
                o = getSp(ctx).getString(key, "");
                break;
            case SP_TYPE_INT:
                o = getSp(ctx).getInt(key, 0);
                break;
            case SP_TYPE_LONG:
                o = getSp(ctx).getLong(key, 0);
                break;
            }
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("static-access")
    public static void saveServerConfigContent(Context context, String serverContent) {
        try {
            if (TextUtils.isEmpty(serverContent) || new JSONObject(serverContent).optInt("suc") == -1
                    || new JSONObject(serverContent).optInt("retCode") == -1) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ServerConfigEntity.getInstance(context).setsInstance(null);//有更新内容，则清除ServerEntity对象
        saveInfo2Sp(context, SP_KEY_SERVER_CONTENT, serverContent);
    }

    public static String getSerVerConfigContent(Context context) {
        try {
            return (String) getInfoFromSp(context, SP_KEY_SERVER_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
